package com.example.flowerpcback.services;

import com.example.flowerpcback.dto.*;
import com.example.flowerpcback.exception.BusinessException;
import com.example.flowerpcback.exception.BusinessExceptionCode;
import com.example.flowerpcback.mapper.MenusMapper;
import com.example.flowerpcback.mapper.MenusMyMapper;
import com.example.flowerpcback.mapper.RolesMenusMapper;
import com.example.flowerpcback.mapper.RolesMenusMyMapper;
import com.example.flowerpcback.req.MenusReq;
import com.example.flowerpcback.req.RolesMenusReq;
import com.example.flowerpcback.req.RolesReq;
import com.example.flowerpcback.resp.MenusResp;
import com.example.flowerpcback.resp.PageResp;
import com.example.flowerpcback.resp.RolesMenusResp;
import com.example.flowerpcback.resp.RolesResp;
import com.example.flowerpcback.utils.CopyUtil;
import com.example.flowerpcback.utils.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RolesMenusService {
    private static final Logger LOG = LoggerFactory.getLogger(RolesMenusService.class);

    @Resource
    private RolesMenusMyMapper rolesMenusMyMapper;

    @Resource
    private RolesMenusMapper rolesMenusMapper;

    @Resource
    private SnowFlake snowFlake;

    public List<RolesMenus> save(List<RolesMenusReq> req){

        if(req.size()>0){
            Long roleId = req.get(0).getRoleId();

            RolesMenusExample rolesMenusExample = new RolesMenusExample();

            RolesMenusExample.Criteria criteria = rolesMenusExample.createCriteria();

            criteria.andRoleIdEqualTo(roleId);

            rolesMenusMapper.deleteByExample(rolesMenusExample);

            List<RolesMenus> rolesMenus = new CopyUtil().copyList(req, RolesMenus.class);

            rolesMenusMyMapper.batchSave(rolesMenus);

            return rolesMenus;
        }else{
            throw  new BusinessException(BusinessExceptionCode.PARAMS_ERROR);
        }

    }

    public  List<RolesMenusResp> list(RolesMenusReq req) {
        RolesMenusExample rolesMenusExample = new RolesMenusExample();
        RolesMenusExample.Criteria criteria = rolesMenusExample.createCriteria();
        if(req.getRoleId() != null) {
            criteria.andRoleIdEqualTo(req.getRoleId());
        }

        List<RolesMenus> res = rolesMenusMapper.selectByExample(rolesMenusExample);


        List<RolesMenusResp> rolesMenusRespList = new CopyUtil().copyList(res,RolesMenusResp.class);

        return rolesMenusRespList;
    }

}
